package com.gitee.zeric.codec.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gitee.zeric.codec.check.BloomFilterHelper;
import com.gitee.zeric.codec.check.CodeUniqueCheck;
import com.gitee.zeric.codec.check.RedisBloomFilter;
import com.gitee.zeric.codec.check.RedisCodeUniqueCheckImpl;
import com.gitee.zeric.codec.parser.CachedCodeRuleParser;
import com.gitee.zeric.codec.parser.DefaultCodeRuleParser;
import com.google.common.base.Charsets;
import com.google.common.hash.Funnel;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @author zj
 * @date 2021/12/17
 */
@Configuration
public class CodeGeneraterAutoConfig {

    @Value("${zcodeprefix:dps_}")
    String zcodeprefix;

    @ConditionalOnMissingBean(name = "redisCodeUniqueCheckImpl")
    //初始化布隆过滤器，放入到spring容器里面
    @Bean
    public BloomFilterHelper<String> initBloomFilterHelper() {
        return new BloomFilterHelper<>((Funnel<String>) (from, into) -> into.putString(from, Charsets.UTF_8).putString(from, Charsets.UTF_8), 1000000, 0.01);
    }

    @ConditionalOnMissingBean(name = "redisCodeUniqueCheckImpl")
    //初始化布隆过滤器，放入到spring容器里面
    @Bean
    public RedisBloomFilter redisBloomFilter(@Qualifier("codecRedisTemplate") RedisTemplate redisTemplate) {
        return new RedisBloomFilter(redisTemplate);
    }

    @ConditionalOnMissingBean(CodeUniqueCheck.class)
    @Bean
    public CodeUniqueCheck redisCodeUniqueCheckImpl(@Qualifier("redisBloomFilter") RedisBloomFilter redisBloomFilter,@Qualifier("initBloomFilterHelper") BloomFilterHelper<String> bloomFilterHelper){
        return new RedisCodeUniqueCheckImpl(redisBloomFilter,bloomFilterHelper);
    }

    @ConditionalOnClass(DefaultCodeRuleParser.class)
    @Bean
    public DefaultCodeRuleParser codeRuleParser(CodeUniqueCheck codeUniqueCheck, @Qualifier("codecRedisTemplate") RedisTemplate redisTemplate, @Qualifier("stringRedisTemplate") StringRedisTemplate stringRedisTemplate){
        return new DefaultCodeRuleParser( codeUniqueCheck,redisTemplate,stringRedisTemplate,zcodeprefix);
    }

    @ConditionalOnClass(CachedCodeRuleParser.class)
    @Bean
    public CachedCodeRuleParser cacheedCodeRuleParser(CodeUniqueCheck codeUniqueCheck, @Qualifier("codecRedisTemplate") RedisTemplate redisTemplate, @Qualifier("stringRedisTemplate") StringRedisTemplate stringRedisTemplate){
        return new CachedCodeRuleParser(codeUniqueCheck,redisTemplate,stringRedisTemplate,zcodeprefix);
    }

//    @ConditionalOnBean(DefaultCodeRuleParser.class)
    @Bean("codecRedisTemplate")
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        RedisSerializer<String> redisSerializer = new StringRedisSerializer();
        template.setKeySerializer(redisSerializer);
        template.setHashKeySerializer(redisSerializer);
        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.setValueSerializer(jackson2JsonRedisSerializer);
        return template;
    }




}
